/** @odoo-module **/
import {registry} from "@web/core/registry";
import {listView} from "@web/views/list/list_view";
import {ListController} from "@web/views/list/list_controller";
import {actionService} from "@web/webclient/actions/action_service";
import {useService} from "@web/core/utils/hooks"

export class GeneInvoice extends ListController {

    setup() {
        super.setup();
        this.action = actionService.start(this.env);
        this.ormService = useService("orm");
        this.notificationService = useService('notification')
    }


    action_to_gene_invoice() {
        // 获取活跃的记录集合,传递给开票模型
        const activeIds = this.model.root.selection.map(record => record.resId);
        if (activeIds.length === 0) {
            this.notificationService.add("请至少选择一个订单开具发票！.", {
                title: "没有记录被选择！",
                type: "warning",
            });
            return;
        }
        var _doAction = this.action.doAction;
        // promise对象回调
        this.ormService.call('rp.ship.order', 'action_aggregate_ship_order', [activeIds]).then(
            // js调用后台向导模型
            result => {
                _doAction(
                    {
                        name: result.name,
                        res_model: result.res_model,
                        views: result.views,
                        type: result.type,
                        view_mode: result.view_mode,
                        target: result.target,
                        context: result.context
                    }
                )
                // this.notificationService.add("开具发票成功！.", {
                //     title: "开具发票成功！",
                //     type: "success",
                // });
            }
        ).catch(
            error => {
                this.notificationService.add(error.data.arguments[0], {
                    title: "失败",
                    type: "warning",
                });
            }
        );


    }

}

registry.category("views").add("gene_invoice", {
    ...listView,
    Controller: GeneInvoice,
    buttonTemplate: "rp_shipment_management.ListView.buttons",
});